<template>
  <el-row justify="end">
    <el-button @click="openForm">
      <Icon class="mr-5px" icon="ep:edit" />
      {{ businessinfo.id ? '编辑工商信息' : '新增工商信息' }}
    </el-button>
  </el-row>
  <el-row :gutter="20">
    <el-col :span="14" :xs="24">
      <ContentWrap>
        <el-collapse v-model="activeNames" class="">
          <el-collapse-item name="basicInfo">
            <template #title>
              <span class="text-base font-bold">基本信息</span>
            </template>
            <el-descriptions :column="4">
              <el-descriptions-item label="统一社会信用代码">{{
                businessinfo.socialCrecode
              }}</el-descriptions-item>
              <el-descriptions-item label="注册地址">{{
                businessinfo.registeredAddress
              }}</el-descriptions-item>
              <el-descriptions-item label="法定代表人">{{
                businessinfo.legalPerson
              }}</el-descriptions-item>
              <el-descriptions-item label="注册资本">{{
                businessinfo.registeredCapital
              }}</el-descriptions-item>
              <el-descriptions-item label="投资性质">
                <dict-tag
                  :type="DICT_TYPE.CRM_INVESTMENT_NATURE"
                  :value="businessinfo.investmentType"
                />
              </el-descriptions-item>
              <el-descriptions-item label="成立日期">
                {{ formatDate(businessinfo.registrationTime, 'YYYY-MM-DD') }}
              </el-descriptions-item>
              <el-descriptions-item label="营业执照">
                <!-- <UploadImgs v-model="businessinfo.businessLicenseUrl" disabled/> -->
                <UploadFile v-model="businessinfo.businessLicenseUrl" disabled  />
              </el-descriptions-item>
              <el-descriptions-item label="身份证复印件">
                <!-- <UploadImgs v-model="businessinfo.idCardUrl" disabled/> -->
                <UploadFile v-model="businessinfo.idCardUrl" disabled  />
              </el-descriptions-item>
            </el-descriptions>
          </el-collapse-item>
        </el-collapse>
      </ContentWrap>
    </el-col>
    <!-- 右侧操作日志 -->
    <el-col :span="10" :xs="24">
      <OperateLogV2 :log-list="logList" />
    </el-col>
  </el-row>

  <label class="font-bold">股东信息</label>
  <BusinessinfoShareholderList
    :key="businessinfoShareholderListRefKey"
    ref="businessinfoShareholderListRef"
    :businessinfo="businessinfo"
  />

  <ContentWrap>
    <el-collapse v-model="activeNames" class="">
      <el-collapse-item name="systemInfo">
        <template #title>
          <span class="text-base font-bold">系统信息</span>
        </template>
        <el-descriptions :column="4">
          <el-descriptions-item label="创建人">{{ businessinfo.creatorName }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ formatDate(businessinfo.createTime) }}
          </el-descriptions-item>
          <el-descriptions-item label="修改人">{{ businessinfo.creatorName }}</el-descriptions-item>
          <el-descriptions-item label="修改时间">
            {{ formatDate(businessinfo.updateTime) }}
          </el-descriptions-item>
        </el-descriptions>
      </el-collapse-item>
    </el-collapse>
  </ContentWrap>
  <!-- 表单弹窗：添加 -->
  <BusinessinfoForm ref="formRef" @success="refresh" />
</template>
<script setup lang="ts">
import { BusinessinfoApi, BusinessinfoVO } from '@/api/crm/businessinfo'
import BusinessinfoShareholderList from '@/views/crm/businessinfo/components/BusinessinfoShareholderList.vue'
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import BusinessinfoForm from '@/views/crm/businessinfo/BusinessinfoForm.vue'
import { BizTypeEnum } from '@/api/crm/permission'
import type { OperateLogVO } from '@/api/system/operatelog'
import { getOperateLogPage } from '@/api/crm/operateLog'
const props = defineProps<{ id?: number; bizId?: number }>()
const route = useRoute()
const businessinfo = ref<BusinessinfoVO>({} as BusinessinfoVO) // 企业工商信息详情
const formLoading = ref(false) // 表单的加载中
const formRef = ref()
const businessinfoShareholderListRefKey = ref(new Date().getTime())
const openForm = () => {
  formRef.value.open(
    businessinfo.value.id ? 'update' : 'create',
    businessinfo.value.id,
    props.bizId
  )
}

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.bizId,
  async (val) => {
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      const data = await BusinessinfoApi.getBusinessinfoByCustomerId(val)
      if (!data) {
        return
      }
      businessinfo.value = data
      await getOperateLog()
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

const activeNames = ref(['basicInfo', 'systemInfo']) // 展示的折叠面板
const refresh = async () => {
  businessinfoShareholderListRefKey.value = new Date().getTime()
  try {
    formLoading.value = true
    const data = await BusinessinfoApi.getBusinessinfoByCustomerId(props.bizId)
    if (!data) {
      return
    }
    businessinfo.value = data
    await getOperateLog()
  } finally {
    formLoading.value = false
  }
}

/** 获取操作日志 */
const logList = ref<OperateLogVO[]>([]) // 操作日志列表
const getOperateLog = async () => {
  if (!businessinfo.value.id) {
    return
  }
  const data = await getOperateLogPage({
    bizType: BizTypeEnum.CRM_BUSINESSINFO,
    bizId: businessinfo.value.id
  })
  logList.value = data.list
}

/** 初始化 */
onMounted(async () => {
  const id = props.id || route.params.id
  const customerId = props.bizId
  try {
    formLoading.value = true
    if (customerId) {
      const data = await BusinessinfoApi.getBusinessinfoByCustomerId(customerId)
      if (!data) {
        return
      }
      businessinfo.value = data
    } else {
      if (id) {
        const data = await BusinessinfoApi.getBusinessinfo(id)
        if (!data) {
          return
        }
        businessinfo.value = data
      }
    }
    await getOperateLog()
  } finally {
    formLoading.value = false
  }
})
</script>
